#!/usr/bin/env python
# -*- coding: utf-8 -*-
from datetime import datetime

from cryptography.fernet import Fernet
import tkinter as tk
from tkinter import messagebox
# 生成密钥
# key = Fernet.generate_key()
# print(key) # b'-AtUkQo5RB3-qd1WW2HaqLJW3Eiq7IjJoBeVeaEDmlU='
# cipher_suite = Fernet(key)

# 要加密的字符串
# text = ["wumeng", "Wumeng123","hkfgjcg","HGHhkf123"]
# cipher_suite = Fernet('-AtUkQo5RB3-qd1WW2HaqLJW3Eiq7IjJoBeVeaEDmlU=')
# # 加密字符串
# for t in text:
#     encrypted_text = cipher_suite.encrypt(t.encode())
#     print(f"{t},加密后的字符串: {str(encrypted_text)}")

#解密字符串
def test():
    cipher_suite = Fernet('-AtUkQo5RB3-qd1WW2HaqLJW3Eiq7IjJoBeVeaEDmlU=')
    encrypted_text = 'gAAAAABnNrFVzftfZG_OcwEPP2gBRWYP4kjgjoaQ7V0A8EMYPLyC6SO0ipPdwHXXsYzievOCP_DGK5IQDYk8o2SX-v6l2DRYsQ=='
    decrypted_text = cipher_suite.decrypt(encrypted_text).decode("utf-8")
    encrypted_text2 = 'gAAAAABnNrh7kCqqVT-QU9HPTQvKYKQVj7v_RyMKLJJWVD6vB6wgpX3TYIgE68Lmc-SfVh9uJNuV2sCSrlML6nh9RafZLQM2cw=='
    decrypted_text2 = cipher_suite.decrypt(encrypted_text2).decode("utf-8") # 变成人能懂的，叫解密
    print(f"解密后的字符串: {decrypted_text}")
    print(f"解密后的字符串: {decrypted_text2}")

def test_dt(date_str:str):
    cipher_suite = Fernet('-AtUkQo5RB3-qd1WW2HaqLJW3Eiq7IjJoBeVeaEDmlU=')
    expiry_date = cipher_suite.decrypt(date_str).decode("utf-8")
    current_time =  datetime.now().strftime("%Y-%m-%d %H:%M:%S")
    print("{}比较{}".format(current_time, expiry_date))
    if current_time > expiry_date:
        print("程序已过期")
        return False
    else: print("程序仍在有效期内")
    return True

def main():
    # 生成密钥
    cipher_suite = Fernet('-AtUkQo5RB3-qd1WW2HaqLJW3Eiq7IjJoBeVeaEDmlU=')

    # 创建主窗口
    root = tk.Tk()
    root.title("加密")
    root.geometry("300x150")
    root.resizable(False, False)
    root.protocol("WM_DELETE_WINDOW", root.destroy)
    root.geometry("+450+200")
    # 创建标签和输入框
    tk.Label(root,width=10, text="用户名:").grid(row=1, pady=8, column=0)
    tk.Label(root,width=10, text="密码:").grid(row=2, column=0)
    tk.Label(root,width=10, text="授权码:").grid(row=3, column=0)

    username_entry = tk.Entry(root,width=25)
    password_entry = tk.Entry(root,width=25)
    token_entry = tk.Entry(root,width=25, show="*")

    username_entry.grid(row=1, pady=8, column=1)
    password_entry.grid(row=2, column=1)
    token_entry.grid(row=3, column=1)

    # 定义加密和显示函数
    def encrypt_and_show():
        if token_entry.get() == "Wy666513%":
            username = username_entry.get()
            password = password_entry.get()
            encrypted_username = cipher_suite.encrypt(username.encode()).decode("utf-8")
            encrypted_password = cipher_suite.encrypt(password.encode()).decode("utf-8")
            messagebox.showinfo("加密后的信息", f"用户名: {username} , 加密后的密码:\n{encrypted_username}\n\n密码: {password} , 加密后的密码:\n{encrypted_password}")
        else:
            messagebox.showerror(title="错误", message="授权码错误，加密失败！！")
    # 创建按钮
    bt = tk.Button(root, text="生成密文", command=encrypt_and_show)#.grid(row=5, column=1)
    bt.place(x=100,y=100)
    def on_press(event):
        encrypt_and_show()
    token_entry.bind("<Return>", on_press)
    # 运行主循环
    root.mainloop()

if __name__ == "__main__":
    main()
    test_dt("gAAAAABnUWijb30WoUgW-XmmYmiC0LEnOTe58PWcc8OHDti5UeMaa8fDXYna7j28EEueflZbGo65v5covYMYpqKQ8bh-kqX2gA==")
    # test_dt("gAAAAABnUWEAGYwj-FKIf4ny5rQ8zB0eGiMEWlQcSS3OMZZqyYr6j-xUVPC2koMHAVvbNVyayiHVHi1-62c5MlLboPGetoTGUA==")
    # 以下授权到2026-12-12
    # gAAAAABn8nhoQ9HQeGsfJFXIUrtlD7N3r7qCxPGd_rc-knd7wYJa_ThdBnvQKX04juFtZ5fXfkqxwIQCJibN5tzeifpidFdiQQ==